<template>
<div class="login">
  <van-nav-bar
  title="登录"
  left-arrow
  @click-left="$router.go(-1)"
/>
  <div class="container">
    <div class="title">
      <h3>手机号登陆</h3>
      <P>未注册的手机号登陆后自动注册</P>
    </div>
    <div class="form">
      <div class="form-item">
        <input class="inp" maxlength="11" placeholder="请输入手机号码" type="text" v-model="mobile">
      </div>
      <div class="form-item">
        <input class="inp" maxlength="5" placeholder="请输入图形验证码" type="text" v-model="picCode">
        <img v-if="picUrl" :src="picUrl" @click="getPicCode">
      </div>
      <div class="form-item">
         <input class="inp" maxlength="11" placeholder="请输入短信验证码" type="text" v-model="smsCode">
         <button @click="getCode">{{ second==totalSecond ? '获取验证码' :second+'秒后重新发送' }}</button>
      </div>
      <div class="login-btn" @click="login">登录</div>

    </div>
  </div>

</div>
</template>
<script>
import { getPicCode, getMsgCode, codeLogin } from '@/api/login.js'
export default {
  name: 'LoginPage',
  data () {
    return {
      picUrl: '',
      picKey: '',
      mobile: '',
      picCode: '',
      smsCode: '',
      totalSecond: 60, // 总秒数
      second: 60, // 倒计时的秒数
      timer: null// 定时器
    }
  },
  methods: {
    async getPicCode () {
      const { data: { base64, key } } = await getPicCode()
      this.picUrl = base64
      this.picKey = key
    },
    async getCode () {
      if (!this.validFn()) {
        return
      }
      if (!this.timer && this.totalSecond === this.second) {
        const res = await getMsgCode(this.picCode, this.picKey, this.mobile)
        console.log(res)
        this.$toast('发送成功，注意查收')
        this.timer = setInterval(() => {
          this.second--
          if (this.second < 1) {
            clearInterval(this.timer)
            this.timer = null
            this.second = this.totalSecond
          }
        }, 1000)
      }
    },
    validFn () {
      if (!/^1[3-9]\d{9}$/.test(this.mobile)) {
        this.$toast('请输入正确的手机号')
        return false
      }
      if (!/^\w{4}$/.test(this.picCode)) {
        this.$toast('请输入正确的图形验证码')
        return false
      }
      return true
    },
    async login () {
      if (!this.validFn()) {
        return
      }
      if (!/^\d{6}$/.test(this.smsCode)) {
        this.$toast('请输入正确的手机验证码')
        return
      }
      const res = await codeLogin(this.mobile, this.smsCode)
      this.$store.commit('user/setUserInfo', res.data)
      const backUrl = this.$route.query.backUrl || '/'
      this.$router.push(backUrl)
      this.$toast('登陆成功')
    }

  },
  created () {
    this.getPicCode()
  },
  destoryed () {
    clearInterval(this.timer)
  }

}
</script>
<style scoped>
.container{
  padding: 49px 29px;
  .title{
    margin-bottom: 20px;
    h3{
      font-size: 26px;
      font-weight: normal;
    }
    p{
      line-height: 40px;
      font-size:14px;
      color: #b8b8b8;
    }
  }

}
.form-item{
  border-bottom: 1px solid #f3f1f2;
  padding: 8px;
  margin-bottom: 14px;
  display: flex;
  align-items: center;
  .inp{
    display: block;
    border: none;
    outline: none;
    height: 32px;
    font-size: 14px;
    flex: 1;
  }
  img{
    width: 94px;
    height: 31px;
  }
  button{
    height: 31px;
    border: none;
    font-size: 13px;
    color:#cea26a;
    background-color: transparent;
    padding-right: 9px;

  }
}
.login-btn{
  width: 100%;
  height: 42px;
  color: #fff;
  background: linear-gradient(90deg,#ecb53c,#ff9211);
  border-radius: 39px;
  box-shadow: 0 10px 20px 0 rgba(0,0,0,.1);
  display: flex;
  justify-content: center;
  margin-top: 39px;
  line-height: 42px;
  align-items: center;
}
</style>
